Tips

An assorted collection of tips and hints. The latest additions and comments will be in this color.

Interactive JavaScript

Load the page
javascript: to enter an interactive evaluation session. Cool!

A word on crashing

As you write and debug your scripts, expect Netscape to crash. A lot. Many bugs will be ironed out for Navigator 3.0. For now, be patient and always, always save any work before testing JavaScript.

And so it was in the first version of this CD-ROM. With the 4.x and 5.x releases of both major browsers, however, this isn't the problem it once was. Microsoft's Internet Explorer can run scripts of 10,000 lines with no difficulties whatsoever.

You might run into the occasional script engine bug these days, but it will be a rare thing.

A word on sound

Although most Netscape browsers support ".au" audio sound files directly, some must run helper applications before the sounds can be played. Keep this in mind as you design your pages. If you assign click sound links to button presses, you may have to wait a minute or so for the press to be "heard"!

Nothing has changed since this was first written. Although the standard sounds formats are well documented and ubiquitous, the browsers still insist on dealing with them through meals of add-ons and plugins. We have no idea why native support for the WAV and MIDI formats hasn't been added--especially since Microsoft was trying so hard to make Internet Explorer the operating system shell in Windows 98.

Images

Always, always, always include HEIGHT and WIDTH tags in your images. Early versions of Netscape Navigator 2.0 will not work correctly with your scripts unless these tags are included.

Although there won't be any versions of Netscape 2.0 running on the web these days (and if there are, too bad for them), it's good practice to make sure you specify the height and width of images. The rendering engines can work much more quickly if they know how large the images are.

Debugging with "alert"

Use JavaScript's "alert()" as you would use C's printf() to keep track of your place and discover where your scripts are going wrong. Be careful, though! Avoid infinite loops or you may have to reboot the computer.

Nothing beats a little "printf debugging." It's no longer necessary to reboot the computer in when the infinite loop occurs, though. Use window.confirm() instead of alert(). It has the same syntax as the alert. The confirmation dialog has a cancel button, which causes the function call to return false. Use that return value to signal a break from the loop.

Line numbers in error reports

Don't even bother trying to match line numbers from error reports with your source code. The line numbers are whimsical at best and completely wrong almost always.

We're happy to report that in most modern cases the error numbers are off by at most one line. It's not a perfect situation, but it's better.

Professional hiding tip #1

Avoid greater-than signs in strings. Set a local variable to the > symbol tmp = unescape('%3E') and add it to your strings: document.write('<SCRIPT'+tmp)

Professional hiding tip #2

Avoid greater-than signs in comparisons. Instead of if (x > 3), use if (3 < x) or if (!(x <= 3)).

Professional hiding tip #3

Avoid decrement in math. Avoid i-- and use i -= 1 instead.

Or the equally viable --i, which isn't going to confuse the HTML parser. Some of you are thinking, what about a line like

if(g < --i) CheckSomething()

and all we have to say is, you've got bigger problems than confused a parser if you're coding with that style!

Alternating quotes

Make sure to alternate your quotes! Even when using the literal escape character (the backslash), for strings within strings, make sure to keep alternating. When those strings evaluate inside functions, methods, or event calls, unalternated strings may fail and cause runtime errors.

Years of scripting experience have led your co-author to suspect that this may be more trouble than it's worth. With all due respect to Erica, Brook believes that sticking with the double quotes and escaped double quotes pays off in the long run. Alternating single and double quotes in a string make it hard for the maintenance programmers to see what's going on in a script.

A little discipline here pays off in the long haul.

Functions with variable arguments

A function's arguments are stored in a property called arguments. You can check the length of this property to tell how many arguments are in use. For example:
function foo()
{
    var argv=foo.arguments
    var argc=foo.arguments.length
}


Copyright ©2000 by Charles River Media, All Rights Reserved